package com.xxz.terminal;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

public class TreeHoleReadActivity extends AppCompatActivity{

    TextView treehole_read,treehole_time,author_name,author_motto;
    DBHelper dbHelper;
    SQLiteDatabase db;
    int [] index;
    Cursor cursor;
    int flag = 0;
    ImageButton treehole_next,treehole_like;
    String nickname,motto,Treehole_time,diarydata,author_id;
    SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tree_hole_read);

        treehole_read = findViewById(R.id.treehole_read);
        treehole_next = findViewById(R.id.treehole_next);
        treehole_time = findViewById(R.id.treehole_time);
        author_name = findViewById(R.id.author_name);
        author_motto = findViewById(R.id.author_motto);
        treehole_like = findViewById(R.id.treehole_like);

        dbHelper = new DBHelper(this);
        db = dbHelper.getWritableDatabase();

        Intent intent = getIntent();
        String PageIndex  = intent.getStringExtra("index");
        if(PageIndex.equals("漫游页面")){
            cursor = db.query("diary_data",new String[]{"diarydata","date","user_id","treehole_time"},"treehole_status=?",new String[]{"已投递"},null
                    ,null,null);
            if(cursor.getCount() == 0){
                treehole_read.setText("抱歉，还没有人投递呀,相信时间会充盈这里");
                treehole_next.setVisibility(View.INVISIBLE);
                treehole_like.setVisibility(View.INVISIBLE);//在做阅读页面时，将按钮隐藏，以免出现错误的时间触发
            }
            else {
                index = generateRandomDistinctArray(cursor.getCount(),0,cursor.getCount());
                cursor.moveToPosition(index[flag]);
                flag++;
                diarydata = cursor.getString(cursor.getColumnIndexOrThrow("diarydata"));
                author_id = cursor.getString(cursor.getColumnIndexOrThrow("user_id"));//非当前用户user_id
                Treehole_time = cursor.getString(cursor.getColumnIndexOrThrow("treehole_time"));
                Cursor cursor1 = db.query("userinformation",new String[]{"nickname","motto"},"user_id=?",new String[]{author_id},null,null
                        ,null,null);
                if(cursor1.moveToNext()){
                    nickname = cursor1.getString(cursor1.getColumnIndexOrThrow("nickname"));
                    motto = cursor1.getString(cursor1.getColumnIndexOrThrow("motto"));
                }

                treehole_read.setText(diarydata);
                treehole_read.setMovementMethod(ScrollingMovementMethod.getInstance());//使得文本框中文字过多时可滑动
                author_name.setText(nickname);
                author_motto.setText(motto);
                treehole_time.setText(Treehole_time);

            }
            treehole_next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(flag<=cursor.getCount()-1){

                        cursor.moveToPosition(index[flag]);
                        flag++;
                        diarydata = cursor.getString(cursor.getColumnIndexOrThrow("diarydata"));
                        Treehole_time = cursor.getString(cursor.getColumnIndexOrThrow("treehole_time"));
                        author_id = cursor.getString(cursor.getColumnIndexOrThrow("user_id"));//非当前用户user_id
                        Treehole_time = cursor.getString(cursor.getColumnIndexOrThrow("treehole_time"));
                        Cursor cursor1 = db.query("userinformation",new String[]{"nickname","motto"},"user_id=?",new String[]{author_id},null,null
                                ,null,null);
                        if(cursor1.moveToNext()){
                            nickname = cursor1.getString(cursor1.getColumnIndexOrThrow("nickname"));
                            motto = cursor1.getString(cursor1.getColumnIndexOrThrow("motto"));
                        }
                        treehole_read.setText(diarydata);
                        treehole_read.setMovementMethod(ScrollingMovementMethod.getInstance());//使得文本框中文字过多时可滑动
                        author_name.setText(nickname);
                        author_motto.setText(motto);
                        treehole_time.setText(Treehole_time);
                    }
                    else {
                        treehole_read.setText("居然都看完了(○´･д･)ﾉ");
                        author_name.setText("树洞");
                        author_motto.setText("期待下一次再会");
                        treehole_time.setText(null);
                        treehole_next.setVisibility(View.INVISIBLE);
                        treehole_like.setVisibility(View.INVISIBLE);//在做阅读页面时，将按钮隐藏，以免出现错误的时间触发

                    }
                }
            });

            treehole_like.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sp = getSharedPreferences("userinformation",MODE_PRIVATE);
                    String user_id = sp.getString("user_id",null);
                    LocalDateTime localDateTime = LocalDateTime.now();
                    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");//用format标准化时间形式
                    String datetime = localDateTime.format(dtf);
                    ContentValues contentValues = new ContentValues();
                    contentValues.put("user_id",user_id);
                    contentValues.put("treehole_diarydata",diarydata);
                    contentValues.put("treehole_date",Treehole_time);
                    contentValues.put("store_date",datetime);
                    contentValues.put("author_id",author_id);
                    db.insert("treehole_diary_likestore",null,contentValues);
                    Toast.makeText(TreeHoleReadActivity.this, "已收藏到我喜欢", Toast.LENGTH_SHORT).show();
                }
            });
        }
        else if(PageIndex.equals("阅读页面")){
            diarydata = intent.getStringExtra("diarydata");
            Treehole_time = intent.getStringExtra("treehole_time");
            nickname = intent.getStringExtra("nickname");
            motto = intent.getStringExtra("motto");

            treehole_read.setText(diarydata);
            treehole_read.setMovementMethod(ScrollingMovementMethod.getInstance());//使得文本框中文字过多时可滑动
            author_name.setText(nickname);
            author_motto.setText(motto);
            treehole_time.setText(Treehole_time);

            treehole_next.setVisibility(View.INVISIBLE);
            treehole_like.setVisibility(View.INVISIBLE);//在做阅读页面时，将按钮隐藏，以免出现错误的时间触发

        }

    }
    // 生成一个长度为n，范围在[rangeL, rangeR]内的不重复的随机数组,以便进行随机树洞漫游
    public static int[] generateRandomDistinctArray(int length, int rangeL, int rangeR){
        if (length <0  || rangeR <= rangeL || (rangeR - rangeL + 1) < length) {
            return null;
        };
        return  new Random().ints(rangeL, rangeR).distinct().limit(length).toArray();
    }

}